var e = getApp(), a = require("../../component/WxNotificationCenter/WxNotificationCenter.js");

Page({
    data: {
      recommendOrder:1,
        schedules: [],
        isLogin: !1,
        pageNo: 1,
        total: -1,
        currentTab: 0,
        provinceIndex: 0,
        provinces: [],
        roleIndex: 0,
        province: "全国",
        roleNames: [],
        roleValues: [],
        newOrderCount: 0,
        searchPageNo: 1,
        listDatas: [],
        empty: " ",
        role: 2,
        shareInfo: []
    },
    loadData: function() {
        var a = this;
        e.network.post({
            url: e.url.listSchedule,
            params: {},
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(t) {
                if (1 == t.data.result) {
                    var o = t.data.data.schedules;
                    a.setData({
                        schedules: o,
                        pageNo: 1
                    });
                } else e.util.showToast(t.data.message);
            },
            fail: function() {
                e.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    loadHistoryData: function() {
        var a = this, t = a.data.pageNo;
        e.network.post({
            url: e.url.historyListSchedule,
            params: {
                pageNo: t,
                pageSize: 20
            },
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(o) {
                if (1 == o.data.result) {
                    var i = a.data.schedules, n = o.data.data.schedules;
                    n.length > 0 ? (i = n.concat(i), t += 1, a.setData({
                        schedules: i,
                        pageNo: t
                    })) : wx.showToast({
                        title: "没有更多历史档期",
                        icon: "none"
                    });
                } else e.util.showToast(o.data.message);
            },
            fail: function() {
                e.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading();
            }
        });
    },
    loadNewOrderData: function() {
        var a = this, t = wx.getStorageSync(e.storage.role);
        void 0 != e.globalData.accessToken && 0 != e.globalData.accessToken.length && 1 != t && e.network.post({
            url: e.url.GetUserNewOrderInfo,
            params: {},
            before: function() {},
            success: function(e) {
                console.log("newOrderCount--\x3e", e), 1 == e.data.result && (a.setData({
                    newOrderCount: e.data.data.newOrderCount ? e.data.data.newOrderCount : ""
                }), e.data.data.recommendOrder && (a.setData({
                    recommendOrder: e.data.data.recommendOrder
                }), a.data.orderAlert = a.selectComponent("#orderAlert"), a.data.orderAlert.show()));
            },
            fail: function() {},
            complete: function() {}
        });
    },
    bindItemClick: function(e) {
        var a = e.currentTarget.dataset.schedule, t = JSON.stringify(a);
        wx.navigateTo({
            url: "/pages/schedule/scheduleDetail/scheduleDetail?schedule=" + t
        });
    },
    clickMyOrder: function(e) {
        wx.navigateTo({
            url: "../order/myOrder/myOrder?currentTab=1"
        });
    },
    clickMyRank: function(e) {
        wx.navigateTo({
            url: "/pages/schedule/myRand/myRand"
        });
    },
    clickInvite: function(e) {
        var a = e.detail;
        console.log(), wx.navigateTo({
            url: "/pages/sendOrder/sendOrderDetail/sendOrderDetail?weddingOrderId=" + a.weddingorderid
        });
    },
    bindPhoneCall: function(e) {
        var a = e.currentTarget.dataset.phone;
        wx.makePhoneCall({
            phoneNumber: a
        });
    },
    bindAddClick: function(a) {
        e.util.isLoginPage() && wx.navigateTo({
            url: "/pages/schedule/addSchedule/addSchedule"
        });
    },
    clickCard: function(a) {
        if (e.util.isLoginPage()) {
            var t = this.data.schedules[this.data.schedules.length - 1], o = t.list[t.list.length - 1].scheduleDate, i = new Date(o);
            console.log(i);
            var n = new Date();
            console.log(n), n > i ? e.util.showToast("您的未来档期为空，请先添加档期") : wx.navigateTo({
                url: "/pages/schedule/scheduleCard/scheduleCard"
            });
        }
    },
    clickSegment0: function(e) {
        this.setData({
            currentTab: 0
        });
    },
    clickSegment1: function(a) {
        e.util.isLoginPage() && (console.log("this.data.newOrderCount", this.data.newOrderCount), 
        this.data.newOrderCount > 0 && (wx.setStorageSync(e.storage.orderProvinceIndex, 0), 
        this.setData({
            provinceIndex: 0
        })), this.setData({
            currentTab: 1,
            newOrderCount: 0
        }), e.network.post({
            url: e.url.LookWeddingOrder,
            params: {},
            before: function() {},
            success: function(a) {
                1 == a.data.result && e.log("设置抢单已看成功");
            },
            fail: function() {},
            complete: function() {}
        }));
    },
    clickLogin: function() {
        e.util.isLoginPage();
    },
    changeProvince: function(a) {
        this.data.provinceIndex != a.detail.value && (wx.setStorageSync(e.storage.orderProvinceIndex, a.detail.value), 
        this.setData({
            provinceIndex: a.detail.value
        }));
    },
    changeRole: function(a) {
        this.data.roleIndex != a.detail.value && (wx.setStorageSync(e.storage.orderRoleIndex, a.detail.value), 
        this.setData({
            roleIndex: a.detail.value
        }));
    },
    didLoginSuccess: function(a) {
        var t = this;
        setTimeout(function() {
            var a = wx.getStorageSync(e.storage.role);
            console.log(a), t.setData({
                isLogin: !0,
                role: a
            }), 1 == a ? (t.loadSearchData(!0), wx.setTabBarItem({
                index: 0,
                text: "查档",
                iconPath: "/images/tab/ic_tab_searchschedule_unselect.png",
                selectedIconPath: "/images/tab/ic_tab_searchschedule_select.png",
                fail: function(e) {
                    console.log("fail:", e);
                },
                success: function(e) {
                    console.log("success:", e);
                }
            })) : (t.loadData(), wx.setTabBarItem({
                index: 0,
                text: "档期",
                iconPath: "/images/tab/ic_tab_schedule_unselect.png",
                selectedIconPath: "/images/tab/ic_tab_schedule_select.png",
                fail: function(e) {
                    console.log("fail:", e);
                },
                success: function(e) {
                    console.log("success:", e);
                }
            }));
        }, 1e3);
    },
    didLogoutSuccess: function(e) {
        this.setData({
            isLogin: !1,
            schedules: [],
            listDatas: [],
            newOrderCount: 0,
            currentTab: 0
        });
    },
    didChangeCityPrice: function(e) {
        if (console.log("didChangeCityPrice!"), 1 == this.data.currentTab && 0 == this.data.provinceIndex) {
            var a = this.selectComponent("#orderlist");
            console.log("orderlist:", a), a.refresh();
        }
    },
    onLoad: function(t) {
        var o = this;
        a.addNotification("updateScheduleList", o.loadData, o), a.addNotification("NeedUpdateOrderAskList", this.updateData, this), 
        a.addNotification("didLoginSuccess", o.didLoginSuccess, o), a.addNotification("logoutSuccess", o.didLogoutSuccess, o), 
        a.addNotification("didChangeCityPrice", o.didChangeCityPrice, o);
        var i = wx.getStorageSync(e.storage.orderProvinceIndex), n = wx.getStorageSync(e.storage.orderRoleIndex);
        this.setData({
            provinces: [ "接单城市" ].concat(e.globalData.provinces),
            roleNames: e.globalData.roleNames,
            roleValues: e.globalData.roleValues,
            provinceIndex: i || 1,
            roleIndex: n || 0
        }), this.loadNewOrderData(), 1 == t.tab && this.setData({
            currentTab: 1
        }), t.inviteCode && (console.log("来自app分享页面：inviteCode:", t.inviteCode), e.globalData.inviteCode = t.inviteCode);
        var s = t.scene;
        s && s.length > 0 && (console.log("scene不为空，请求", s), e.util.inviteFromWeixinProgramCodeSceneValue(s)), 
        e.util.getUserInfo(), e.util.openSharePage(t);
    },
    onReady: function() {},
    onShow: function() {
        1 == wx.getStorageSync(e.storage.role) ? wx.setNavigationBarTitle({
            title: "查档"
        }) : wx.setNavigationBarTitle({
            title: "档期"
        }), e.globalData.currentTab = "/pages/schedule/schedule", wx.showTabBar({
            fail: function(e) {
                console.log("showTabBar fail!!!:", e);
            }
        }), e.util.isFirstLogin() && this.loadData(), this.setData({
            isLogin: e.util.isLogin()
        }), e.util.isLogin() ? this.didLoginSuccess() : this.setData({
            schedules: []
        });
    },
    onHide: function() {},
    onUnload: function() {
        a.removeNotification("updateScheduleList", this), a.removeNotification("NeedUpdateOrderAskList", this), 
        a.removeNotification("didLoginSuccess", this), a.removeNotification("logoutSuccess", this), 
        a.removeNotification("didChangeCityPrice", this);
    },
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh(), e.log("刷新"), 0 != e.util.isLogin() && 1 != this.data.currentTab && (1 == this.data.role ? this.loadSearchData(!0) : this.loadHistoryData());
    },
    onReachBottom: function() {},
    onShareAppMessage: function(a) {
        var t = this;
        if (void 0 != (o = a.target)) return o = a.target.dataset.detail.weixinProgramShareInfo, 
        console.log("shareInfo", o), {
            title: o.shareTitle,
            path: o.shareUrl,
            imageUrl: o.shareImage,
            success: function(e) {
                t.data.shareInfo = [];
            },
            fail: function(e) {
                t.data.shareInfo = [];
            }
        };
        var o = e.util.getShareInfo();
        return {
            title: e.globalData.commonShareTitle,
            path: o[0],
            imageUrl: o[1],
            success: function(e) {},
            fail: function(e) {}
        };
    },
    clickSearch: function(e) {
        wx.navigateTo({
            url: "/pages/add/addOrder/addOrder"
        });
    },
    orderLoadMore: function(a) {
        e.util.isLogin() && this.loadSearchData(!1);
    },
    orderRefresh: function(a) {
        e.util.isLogin() && this.loadSearchData(!0);
    },
    clickItem: function(e) {
        var a = e.currentTarget.dataset.index, t = e.currentTarget.dataset.orderid, o = this.data.listDatas;
        o[a].newCount = 0, this.setData({
            listDatas: o
        }), console.log("aa" + this.data.listDatas), wx.navigateTo({
            url: "/pages/sendOrder/orderDetail/orderDetail?weddingOrderId=" + t
        });
    },
    loadSearchData: function(a) {
        var t = this, o = t.data.searchPageNo;
        if (a) o = 1; else if (t.data.total == t.data.listDatas.length) return;
        e.network.post({
            url: e.url.ListPlannerWeddingOrder,
            params: {
                pageNo: o,
                pageSize: 20
            },
            before: function() {
                wx.showNavigationBarLoading();
            },
            success: function(i) {
                if (1 == i.data.result) {
                    var n = [];
                    a || (n = t.data.listDatas), n = n.concat(i.data.data.weddingOrders), console.log(n), 
                    o += 1, t.setData({
                        total: i.data.data.total,
                        listDatas: n,
                        searchPageNo: o
                    });
                } else e.util.showToast(i.data.message);
            },
            fail: function() {
                e.util.showToast("获取数据失败");
            },
            complete: function() {
                wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
            }
        });
    },
    updateData: function() {
        this.loadSearchData(!0);
    }
});